package game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author Fredrik
 *
 */
public class GuessingPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1919721246013946813L;
	
	private MainModel model;
	private MapPanel mapPanel;
	private JTextField guessWord = new JTextField();
	private JButton guessButton = new JButton("Guess");
	private JPanel gPanel = new JPanel();
	
	/**
	 * Creates a guessing panel
	 * @param model
	 * @param controller
	 */
	public GuessingPanel(MainModel model, MainController controller) {
		this.model = model;
		this.setBackground(Color.ORANGE);
		this.guessWord.setEditable(true);
		this.gPanel.setBackground(this.getBackground());
		this.mapPanel = new MapPanel(model,controller);
		this.gPanel.setBorder(BorderFactory.createTitledBorder("Guess"));
		this.gPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.NORTHWEST;
		c.fill = GridBagConstraints.HORIZONTAL;
		this.gPanel.add(this.guessWord,c);
		c.gridx += 1;
		this.gPanel.add(this.guessButton,c);
		this.setLayout(new BorderLayout());
		this.add(this.mapPanel,BorderLayout.NORTH);
		this.add(this.gPanel,BorderLayout.SOUTH);
		
		this.guessButton.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				guess();
			}});
		
		this.guessWord.addKeyListener(new KeyListener(){

			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					guess();
				}
				
			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}});
		
	}
	
	private void guess(){
		
		
		if(this.model.guessAtWord(this.guessWord.getText())){
			this.model.changed();
		}
		this.guessWord.setText("");
		
	}
}
